package bitArithmetic;
//191. 位1的个数
public class basic191 {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int count = 0;
        while(n != 0){
            //将最右边的1设为0
            n = n & (n - 1);
            count++;
        }
        return count; //返回计数结果


//        int res = 0;
//        for (int i = 0; i < 32; i ++){
//            //取某一个二进制位 不等于0  注意不能写等于1，因为是二进制位的1
//            if ((n & (1 << i)) != 0){
//                res ++;
//            }
//        }
//        return res;

    }
    public static void main(String[] args) {
        basic191 basic191 = new basic191();
        System.out.println(basic191.hammingWeight(13));

    }
}
